package br.kanban.view.service;

import java.io.IOException;
import java.io.Serializable;

import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import br.kanban.core.busines.UsuarioBO;
import br.kanban.core.persistence.Usuario;
import br.kanban.core.persistence.Usuario.Roles;


@Named
@SessionScoped
public class Identity implements Serializable{

	private static final long serialVersionUID = 1L;

	private Usuario usuarioLogado;

	@Inject
	private UsuarioBO usuarioBO;

	private String username;
	
	private String password;
	private String newPassword;
	private String newPassword2;
	
	public void login() throws IOException{
		if(username != null && password != null){
			usuarioLogado = usuarioBO.pesquisar(username);
			
			if(usuarioLogado != null && usuarioLogado.validaSenha(password)){
				FacesContext.getCurrentInstance().getExternalContext().redirect("/Kanban/paginas/"); 
				username = "";
				password = "";
				return;
			}
		}
		
		FacesContext.getCurrentInstance().addMessage(null, 
				new FacesMessage(FacesMessage.SEVERITY_ERROR, "Usuário ou senha inválido", null));
	}

	public void logout() throws IOException{
		usuarioLogado = null;
		FacesContext.getCurrentInstance().getExternalContext().redirect("/Kanban/");
	}
	
	public void alterarSenha(){
		if(usuarioLogado.validaSenha(password)){
			if(newPassword != null && newPassword.equals(newPassword2)){
				usuarioLogado.setSenha(newPassword);
				usuarioBO.update(usuarioLogado);
				FacesContext.getCurrentInstance().addMessage(null, 
						new FacesMessage(FacesMessage.SEVERITY_INFO, "Senha alterada com sucesso", ""));
			}else{
				FacesContext.getCurrentInstance().addMessage(null, 
						new FacesMessage(FacesMessage.SEVERITY_ERROR, "Confirmação de senha difere da senha nova", ""));				
			}
		}else{
			FacesContext.getCurrentInstance().addMessage(null, 
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "Senha atual inválida", ""));
		}
		password = newPassword = newPassword2 = "";
	}
	
    public boolean rendered(String page){
    	if(usuarioLogado.getCargo() == Roles.ADMIN) return true;
    	if(page.equals("usuario") || page.equals("cliente")) return false;
    	
    	if(page.equals("projeto") || page.equals("sprint") || page.equals("relatorio")){
    		return usuarioLogado.getCargo() == Roles.MASTER;
    	}else if(page.equals("tarefa")){
    		return usuarioLogado.getCargo() == Roles.MASTER || usuarioLogado.getCargo() == Roles.ANALISTA;
    	}else if(page.equals("cadastro")){
    		return usuarioLogado.getCargo() != Roles.DESENVOLVE && usuarioLogado.getCargo() != Roles.TESTER;
    	}
    	
    	return true;
    }

	public String getUsername() {
		return username;
	}

	public void setUsername(String userName) {
		this.username = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public Usuario getUsuarioLogado(){
		return usuarioLogado;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getNewPassword2() {
		return newPassword2;
	}

	public void setNewPassword2(String newPassword2) {
		this.newPassword2 = newPassword2;
	}
	
}
